package com.team24.hotel.controller;

import com.team24.hotel.domain.Good;
import com.team24.hotel.domain.Supplier;
import com.team24.hotel.mapper.SupplierMapper;
import com.team24.hotel.service.GoodService;
import com.team24.hotel.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private SupplierService supplierService;

    @GetMapping("/list")
    public String listGoodPage(Model model) {
        List<Good> goods = goodService.listGood();
        model.addAttribute("goods",goods);
        return "goodList";
    }

    @RequestMapping(value = "toAdd",method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("good",new Good());
        List<Supplier> suppliers=supplierService.listSupplier();
        model.addAttribute("suppliers",suppliers);
        return "goodAdd";
    }

    @RequestMapping("/delete")
    public String deleteGoodById(Integer id){
        goodService.deleteGoodByPrimaryKey(id);
        return "redirect:/good/list";
    }

    @RequestMapping(value = "/findById")
    public String findGoodById(Model model,Integer id){
        Good good = goodService.findGoodById(id);
        List<Supplier> suppliers=supplierService.listSupplier();
        model.addAttribute("suppliers",suppliers);
        model.addAttribute("good",good);
        return "goodAdd";
    }

    @RequestMapping(value = "/save")
    public String saveGood(Good good) {
        goodService.saveOrUpdate(good);
        return "redirect:/good/list";
    }
}



